
import React from 'react';

import styles from './PointList.css';
import {Table, message, Form, Input, Icon, Button} from 'antd';
import PointService from "../../business/point/PointService";
import {WithdrawPointDomain} from "../../business/rule/WithdrawPointDomain";
import {PointsDomain} from "../../business/point/PointsDomain";

const rowSelection = {
  onChange: (selectedRowKeys, selectedRows) => {
  },
  getCheckboxProps: record => ({
    disabled: record.name === 'Disabled User',
    name: record.name,
  }),
};

const getNowDate =()=>{
  const date = new Date();
  let month: string | number = date.getMonth() + 1;
  let strDate: string | number = date.getDate();

  if (month <= 9) {
    month = "0" + month;
  }

  if (strDate <= 9) {
    strDate = "0" + strDate;
  }

  return date.getFullYear() + "-" + month + "-" + strDate + " "
    + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();
}

class PointList extends React.Component{

  constructor(props) {
    super(props);
    this.pointService =new PointService();
    this.callBackQueryList = this.callBackQueryList.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
    this.inputUsername = this.inputUsername.bind(this);
    this.withdrawCallback = this.withdrawCallback.bind(this);


  }
  pointService:PointService;
  state ={
    giftData:[],
    username:'',
  }

  columns = [{
    title: '记录ID',
    dataIndex: 'recordId',
    key: 'recordId',
  }, {
    title: 'UMID',
    dataIndex: 'umId',
    key: 'umId',
  }, {
    title: '创建时间',
    dataIndex: 'creationTime',
    key: 'creationTime',
  }, {
    title: '详情',
    dataIndex: 'recordDetails',
    key: 'recordDetails',
  }, {
    title: '积分',
    dataIndex: 'points',
    key: 'points',
  }, {
    title: '类型',
    dataIndex: 'type',
    key: 'type',
    render:(text, record)=>{
      if(text =='reward_points'){
        return '获得积分';
      }else if(text =='withdraw_reward'){
        return '获得积分撤销';
      }else if(text=="consume_points"){
        return "商城消费";
      }else if(text=="withdraw_consume"){
        return "商城消费撤销";
      }
      return text;
    }
  }, {
    title: '产生者',
    dataIndex: 'createdBy',
    key: 'createdBy',
  }, {
    title: '操作',
    dataIndex: 'type',
    key: 'action',
    render:(text, record)=>{
      if(text =='reward_points'){
        return <a href="javascript:void(0);" onClick={()=>this.rewardPoint(record,'withdraw_reward')}>撤销</a>
      }else if(text =='consume_points'){
        return <a href="javascript:void(0);" onClick={()=>this.rewardPoint(record,'withdraw_consume')}>撤销</a>
      }else{
        return '';
      }
    }
  }];


  rewardPoint(record:PointsDomain,operateType:string){
    let username = sessionStorage.getItem('adminUsername');
    let withdraw = new WithdrawPointDomain();
    withdraw.recordId = record.recordId;
    withdraw.umId= record.umId;
    withdraw.operateTime =getNowDate();
    withdraw.operateType = operateType;
    withdraw.operator =username;
    let token = sessionStorage.getItem('adminLoginToken');
    if(operateType =='withdraw_reward'){
      this.pointService.withdrawPoint(withdraw,token,this.withdrawCallback);
    }else{
      this.pointService.withdrawConsumePoint(withdraw,token,this.withdrawCallback);
    }
  }

  withdrawCallback(res,isSuccess){
    if(!isSuccess || res.code !="200"){
      message.info(res.message);
    }else{
      let token = sessionStorage.getItem('adminLoginToken');
      const {username} =this.state;
      this.pointService.queryRecordList(username,[],token,this.callBackQueryList);
    }
  }

  callBackQueryList(giftList,isSuccess){
    if(!isSuccess || giftList.code !="200"){
      message.info(giftList.message);
    }else{
      this.setState({giftData: giftList.data});
    }
  }

  show(giftData:any[]){
    if(giftData.length !=0){
     return (<Table columns={this.columns} dataSource={giftData} rowKey={record=>record.recordId} />);
    }else{
      return "";
    }
  }
  inputUsername(e){
    this.setState({
      [e.target.name] :e.target.value,
    });
  }
  handleSubmit(e){
    e.preventDefault();
    let token = sessionStorage.getItem('adminLoginToken');
    const {username} =this.state;
    if(username ==''){
      message.info("请输入UMID");
    }
    this.pointService.queryRecordList(username,[],token,this.callBackQueryList);
  }

  render() {
    const {giftData} = this.state;
    return (
      <div className={styles.normal}>
        <Form layout="inline" onSubmit={this.handleSubmit}>
            <Form.Item
              label={"查询用户的um_id"}
            >
              <Input onChange={this.inputUsername} prefix={<Icon type="user" style={{ color: 'rgba(0,0,0,.25)' }} />}  placeholder="请输入UM_ID" name="username" />
            </Form.Item>
            <Form.Item>
              <Button
                type="primary"
                htmlType="submit"
              >
                查询
              </Button>
            </Form.Item>
        </Form>
        {this.show(giftData)}
      </div>
    );
  }
}

export default PointList;
